<template>
  <div class="out">
    <zHeader pageTitle="访客预约"></zHeader>

    <div class="pageOut">
        <div class="topOptionArea">
            <van-search v-model="inputSearchContent" shape="round" class="searchBox" placeholder="请输入手机号或名字" />
            <div class="levelSelectbtn" @click="ifShowDatetimePicker = true">
                {{ showDate }}
                <svg t="1743939109255" class="iconfont" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1428" width="10" height="10">
                    <path d="M511.999488 634.683157 132.797004 255.480672l-66.916039 66.916039 446.1175 446.122617 446.1175-446.122617-66.916039-66.916039L511.999488 634.683157zM511.999488 634.683157" fill="#272636" p-id="1429"></path>
                </svg>
            </div>
            <van-popup :show="ifShowDatetimePicker" position="bottom" @close="ifShowDatetimePicker = false">
                <van-date-picker
                    v-model="currentDate"
                    @confirm="onDateSelectHandler"
                    @cancel="ifShowDatetimePicker = false"
                />
            </van-popup>
            <van-button class="addBtn" type="primary" round color="#187eff" @click="loadKanFangYuYueListHandler">查询</van-button>
        </div>

        <div class="listArea">
            <div class="listItemCard" v-for="item in kanFangYuYueList" :key="item.kanFangYuYueId">
                <div class="topArea">
                    <div class="nameArea">
                        <div class="name">{{ item.xingMing }}</div>
                        <div class="xingBie">{{ item.xingBieText }}</div>
                    </div>
                    <div class="date">{{ item.yuYueDate }}</div>
                </div>
                <div class="otherInfoArea">
                    <span class="tipText">手机号: </span>
                    {{ item.shouJiHao }}
                </div>
                <div class="otherInfoArea">
                    <span class="tipText">预约时间: </span>
                    {{ item.yuYueTime }}
                </div>
                <div class="otherInfoArea">
                    <span class="tipText">预约人数: </span>
                    {{ item.yuYueNum }}人
                </div>
            </div>

            <view class="noData" v-if="kanFangYuYueList.length == 0">
                暂无预约
            </view>
        </div>
    </div>
    
  </div>
</template>

<script setup>
import { pageConstant } from '@/config/contant.js'
import { postKanFangYuYueList } from '@/api/kanFangyuyue/index.js'
import { useRouter } from 'vue-router'
const router = useRouter()
import { getSelectedXiaoQuId } from '@/config/config.js'

if (!getSelectedXiaoQuId()) {
  showDialog({
    message: '暂无小区数据，请先配置小区',
  }).then(() => {
    router.back()
  })
}

/**
 * 获取看房预约数据
 */
const formatTimestampToYYYYMMDD = (timestamp) => {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
    const day = String(date.getDate()).padStart(2, '0');
    
    return `${year}-${month}-${day}`;
}
const inputSearchContent = ref('')
const showDate = ref(formatTimestampToYYYYMMDD(new Date()))
const kanFangYuYueList = ref([])
const loadKanFangYuYueListHandler = () => {
    showLoadingToast({
        message: '加载中...',
        forbidClick: true,
        duration: 0
    });

    let param = {
        query: inputSearchContent.value,
        yuYueDate: showDate.value,
        xiaoQuId: getSelectedXiaoQuId()
    }

    postKanFangYuYueList(param).then(res => {
        // 关闭加载中提示
        closeToast();

        if (res.data.code == pageConstant.okCode) {
            kanFangYuYueList.value = res.data.data
            kanFangYuYueList.value.forEach(item => {
                item.xingBieText = pageConstant.fangKeXingBie.filter(data => item.xingBie == data.value)[0].key
            })
        } else {
            showToast(res.data.msg)
        }
    })
    .catch((err) => {
        console.log(err)
    })
}
loadKanFangYuYueListHandler()

/**
 * 搜索功能
 */
const currentDate = ref([new Date().getFullYear(), String(new Date().getMonth() + 1).padStart(2, '0'), String(new Date().getDate()).padStart(2, '0')])
const ifShowDatetimePicker = ref(false)
const onDateSelectHandler = (event) => {
    ifShowDatetimePicker.value =  false
    showDate.value = event.selectedValues.join('-')
}
</script>

<style lang="scss" scoped>
.out {
    min-height: 100dvh;
    width: 100vw;
    background-color: rgb(241, 241, 241);

    .pageOut {
        width: 100%;

        .topOptionArea {
            width: 100%;
            height: 40px;
            background: #FFFFFF;
            display: flex;
            padding: 6px 0;
            align-items: center;
            overflow: hidden;

            .searchBox {
                width: 220px;

                ::v-deep .van-search {
                    padding: 0;
                }

                ::v-deep .van-cell {
                    padding: 0;
                    background-color: transparent;
                }
            }

            .levelSelectbtn {
                padding: 4px 10px 4px 0;
                border-radius: 5px;
                display: flex;
                align-items: center;

                .iconfont {
                    padding-left: 4px;
                    padding-bottom: 2px;
                    font-size: 15px;
                }
            }

            .addBtn {
                width: 64px;
                height: 31px;

                ::v-deep(.van-button) {
                    width: 64px;
                    height: 31px;
                    border-radius: 26px 26px 26px 26px;
                    line-height: 31px;
                    font-weight: 500;
                }
            }
        }

        .listArea {
            width: 100%;
            padding: 16px;
            padding-top: 0;
            box-sizing: border-box;

            .listItemCard {
                width: 100%;
                padding: 16px;
                padding-top: 12px;
                padding-bottom: 0;
                box-sizing: border-box;
                margin-top: 12px;
                background: #FFFFFF;
                border-radius: 8px 8px 8px 8px;

                .topArea {
                    display: flex;
                    padding-bottom: 15px;
                    justify-content: space-between;
                    
                    .nameArea {
                        width: calc(100% - 150px);
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        font-size: 16px;
                        color: #303133;
                        line-height: 38rpx;
                        font-weight: bold;

                        display: flex;

                        .name {
                            padding-right: 30px;
                        }
                    }

                    .date {
                        font-weight: 400;
                        font-size: 14px;
                        color: #4E5969;
                        line-height: 15px;
                    }
                }

                .otherInfoArea {
                    padding-bottom: 15px;
                    font-size: 15px;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    width: 100%;

                    .tipText {
                        font-weight: 400;
                        color: #4E5969;
                        line-height: 17px;
                    }
                }
            }

            .noData {
                height: 100px;
                color: #c0c0c0;
                display: flex;
                flex-flow: row nowrap;
                justify-content: center;
                align-items: center;
            }
        }
    }
}
</style>